package com.ontheroad.controller;

import cn.modoumama.page.GridDataModel;
import com.ontheroad.activity.model.ActivityModel;
import com.ontheroad.activity.model.ActivityTypeModel;
import com.ontheroad.activity.service.ActivitService;
import com.ontheroad.activity.service.ActivityTypeService;
import com.ontheroad.activity.service.app.AppActivityService;
import com.ontheroad.api.request.ActivityRequest;
import com.ontheroad.api.response.Response;
import com.ontheroad.core.util.DateUtils;
import com.ontheroad.core.util.StringUtils;
import com.ontheroad.core.web.BaseController;
import com.ontheroad.user.model.UserModel;
import com.ontheroad.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/27.
 */
@Controller
@RequestMapping("activity")
public class ActivityController extends BaseController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActivitService activitService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityTypeService typeService;
    @Autowired
    private AppActivityService appActivityService;


    @RequestMapping("init")
    public String init() {
        return "activity/init";
    }

    @RequestMapping("getData")
    @ResponseBody
    public GridDataModel getData(HttpServletRequest request) {
        GridDataModel model = getGridData(request, activitService);

        try {
            if (model.getTotal() > 0) {
                List<ActivityModel> activitys = (List<ActivityModel>) model.getRows();
                for (ActivityModel activity : activitys) {
                    Integer userId = activity.getCreateUserId();
                    // 查询用户
                    UserModel user = userService.findById(userId);
                    String nick = user.getNick();
                    if (StringUtils.isBlankString(nick)) {
                        nick = user.getMobile();
                    }
                    activity.setNick(nick);
                    // 查询类型
                    List<ActivityTypeModel> typelist = typeService.findByActivityId(activity.getId());
                    StringBuilder types = new StringBuilder();
                    if (typelist != null && typelist.size() > 0) {
                        for (ActivityTypeModel activityTypeModel : typelist) {
                            types.append(activityTypeModel.getName()).append(",");
                        }
                        if (types.length() > 0) {
                            types = new StringBuilder(types.substring(0, types.length() - 1));
                            activity.setTypes(types.toString());
                        }
                    }
                }
                model.setRows(activitys);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @RequestMapping("detail")
    public String detail(Map map, Integer id) throws Exception {
        ActivityRequest request = new ActivityRequest();
        request.setActivityId(id);
        Response response = appActivityService.detail(request);
        map.putAll(response.getResultMap());
        if (map.containsKey("startTime")) {
            Long startTime = (Long) map.get("startTime");
            map.put("startTime", DateUtils.timestampToStringDate(startTime));
        }
        if (map.containsKey("endTime")) {
            Long endTime = (Long) map.get("endTime");
            map.put("endTime", DateUtils.timestampToStringDate(endTime));
        }
        if (map.containsKey("costType")) {
            Integer costType = (Integer) map.get("costType");
            if (costType != null) {
                switch (costType) {
                    case 1:
                        // AA
                        break;
                    case 2:
                        map.put("cost", "免费");
                        break;
                    case 3:
                        map.put("cost", "商用");
                        break;
                }
            }
        }

        if (map.containsKey("types")) {
            List<ActivityTypeModel> activityTypeModels = (List<ActivityTypeModel>) map.get("types");
            StringBuilder types = new StringBuilder();
            if (activityTypeModels != null && activityTypeModels.size() > 0) {
                for (ActivityTypeModel activityTypeModel : activityTypeModels) {
                    types.append(activityTypeModel.getName()).append("，");
                }
                types = new StringBuilder(types.substring(0, types.length() - 1));
            }
            map.put("types", types.toString());
        }


        return "activity/createActive";
    }

    @RequestMapping(value = "delete")
    public void delete(HttpServletRequest request, PrintWriter out) {
        log.debug("method: delete() ");
        String msg = "操作成功";
        boolean result = true;
        try {
            String ids = request.getParameter("ids");
            String[] aDeleteId = ids.split(",");
            for (String id : aDeleteId) {
                activitService.removeById(Integer.valueOf(id));
            }
        } catch (Exception e) {
            msg = "系统发生异常！";
            result = false;
        }
        ajaxJsonResponse(out, result, msg);
    }
}
